package com;

import java.util.*;

public class Landlords {

    //用作排序时取字符串下标做比较
    private static String temp="大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3";

    static void sorter(ArrayList<String> p){
        p.sort(new Comparator<String>() {
            @Override
            public int compare(String t1, String t2) {
                String s1=t1.substring(2);
                String s2=t2.substring(2);
                if(s1.equals(""))
                    s1=t1;
                if(s2.equals(""))
                    s2=t2;
                int a=temp.indexOf(s1);
                int b=temp.indexOf(s2);
                return a-b;
            }
        });
    }

    public static void main(String[] args) {
        String[] num = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        String[] color = {"黑桃","红桃","方片","麻花"};
        //存储54张牌
        ArrayList<String> list = new ArrayList<>();
        int i=0;
        for(String s1:num){
            for(String s2:color){
                list.add(i++,s2+s1);
            }
        }
        list.add(i++,"大王");
        list.add(i++,"小王");

        //洗牌
        Collections.shuffle(list);
        //创建三名玩家
        ArrayList<String> p1 = new ArrayList<>();
        ArrayList<String> p2 = new ArrayList<>();
        ArrayList<String> p3 = new ArrayList<>();

        //打印发牌过程
        int cnt=0;
        for(i=0;i<51;i++){
            cnt%=3;
            String str = list.get(i);
            int tt=cnt+1;
            System.out.println("第"+tt+"位玩家得到："+str);
            switch (cnt){
                case 0:p1.add(str);break;
                case 1:p2.add(str);break;
                case 2:p3.add(str);break;
            }
            cnt++;
        }
        System.out.println();

        //最后剩余的三张为底牌
        System.out.print("底牌为：");
        for(i=51;i<54;i++){
            System.out.print(list.get(i)+" ");
        }
        System.out.println("\n");

        System.out.println("未排序前：");
        System.out.print("第1位玩家牌为：");
        System.out.println(p1);
        System.out.print("第2位玩家牌为：");
        System.out.println(p2);
        System.out.print("第3位玩家牌为：");
        System.out.println(p3);
        //对三位玩家手中的牌进行排序再输出
        sorter(p1);
        sorter(p2);
        sorter(p3);

        System.out.println("排序后：");
        System.out.print("第1位玩家牌为：");
        System.out.println(p1);

        System.out.print("第2位玩家牌为：");
        System.out.println(p2);

        System.out.print("第3位玩家牌为：");
        System.out.println(p3);
    }
}
